package cn.ekuma.epos.analysis.customer;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.event.EventListenerList;

import net.sourceforge.jtimepiece.TimeUtil;

import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;

import cn.ekuma.epos.analysis.customer.model.CustomerAnalysisTableModel;
import cn.ekuma.epos.analysis.product.model.ProductAnalysisTableModel;
import cn.ekuma.epos.businesslogic.OrderUtil;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.I_DataLogicSales;

import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.OrderType;
import com.openbravo.bean.erp.viewbean.CustomerAnalysisInfo;
import com.openbravo.bean.erp.viewbean.CustomerTransaction;
import com.openbravo.bean.erp.viewbean.OrderLine_V;
import com.openbravo.bean.erp.viewbean.ProductAnalysis;
import com.openbravo.bean.erp.viewbean.ProductTransaction;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.pos.panels.event.ProductChangedListener;

public class AnalysisModelManager {
	private ArrayList<String> transactionColumns;
    CustomerAnalysisTableModel tableModel;
    List<CustomerAnalysisInfo> customerAnalysis;
    List<CustomerTransaction> customerTransaction;
    private I_DataLogicERP dlSales;
    
    
    public AnalysisModelManager(I_DataLogicERP dlSales) {
        this.dlSales=dlSales;
        transactionColumns=new ArrayList<String>();
        tableModel=new CustomerAnalysisTableModel();
    }

    public void query(QBFParameters analysisFilter,QBFParameters transactionFilter) throws BasicException{
           
            customerAnalysis = dlSales.query(CustomerAnalysisInfo.class,analysisFilter);
            customerTransaction =dlSales.customerTransactionListQBF(transactionFilter);
            customerTransaction.addAll(dlSales.customerPOSTransactionListQBF(transactionFilter));
            fillProductTransations();
            tableModel.resert(transactionColumns,customerAnalysis);
    }

    //算法应改进
    private void fillProductTransations(){
            transactionColumns.clear();
            String realOrderName;
            for(CustomerTransaction pt:customerTransaction){
              realOrderName=pt.getOrderTypeName()+"("+(pt.getOrderType()==0?OrderUtil.ORDERTYPE_Normal:OrderUtil.ORDERTYPE_Revoerse)+")";
              if(!transactionColumns.contains(realOrderName))
                    transactionColumns.add(realOrderName);
                 for(CustomerAnalysisInfo pa:customerAnalysis){
                    if(pt.getCustomerId().equalsIgnoreCase(pa.getId()))
                        pa.putTransaxtion(realOrderName, pt);
                }
        }
    }

    public ArrayList<String> getTransactionColumns() {
        return transactionColumns;
    }

    public I_DataLogicSales getDlSales() {
        return dlSales;
    }

	public CustomerAnalysisTableModel getTableModel() {
		return tableModel;
	}

	public void setTableModel(CustomerAnalysisTableModel tableModel) {
		this.tableModel = tableModel;
	}
}
